package org.haredot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.haredot.entity.ServiceLog;
import org.haredot.mapper.LoggerMapper;
import org.haredot.service.LoggerService;
import org.haredot.vo.PageVo;
import org.haredot.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoggerServiceImpl implements LoggerService {

    @Resource
    private LoggerMapper loggerMapper ;

    @Override
    public ResultVo<?> queryServiceLogs(PageVo<ServiceLog> pageVo) {

        Map<String, ?> params = pageVo.getParams();

        PageInfo<ServiceLog> info = PageHelper.startPage(pageVo)
                .doSelectPageInfo(()-> loggerMapper.findAll(params));

        pageVo.setTotal(info.getTotal()).setList(info.getList());

        return ResultVo.ok().data(pageVo).build();
    }

    @Override
    public ResultVo<?> queryLoginLogs(PageVo<ServiceLog> pageVo) {

        Map<String, ?> params = pageVo.getParams();

        PageInfo<ServiceLog> info = PageHelper.startPage(pageVo)
                .doSelectPageInfo(()-> loggerMapper.findLogsAll(params));

        pageVo.setTotal(info.getTotal()).setList(info.getList());

        return ResultVo.ok().data(pageVo).build();
    }
}
